<?php
/*------------------------------------------------------------------------
 * UserFreeOrderController.class.php 
 *
 * 免费合同订单
 * 	
 * Created on 2016-01-03
 *
 * Author: hwc123 <hwc123@139.com>
 * 
 * Copyright (c) 2015 http://www.qingtajiao.com All rights reserved.
 * ------------------------------------------------------------------------
 */

namespace Home\Controller;
class UserFreeOrderController extends CommonController{

	public function _initialize(){
		parent::_initialize();
		$this->user_id();
		$this->add_step('合同管理', U('index'));
	}
	// 订单列表
	public function index(){
		$page      = $this->_get('p', '', 1);
		$pagesize  = 10;
		$first_row = $pagesize * ($page - 1);

		// 订单号
		$order_sn = $this->_get('order_sn');
		if($order_sn){
			$where['order_sn'] = $order_sn;
		}
		$this->assign('order_sn', $order_sn);

		// 开始时间
		$start_date = $this->_get('start_date');
		if($start_date){
			$where['create_time'][] = array('egt',strtotime($start_date));
		}
		$this->assign('start_date', $start_date);

		// 结束时间
		$end_date = $this->_get('end_date');
		if($end_date){
			$where['create_time'][] = array('elt',strtotime($end_date . '23:59:59'));
		}
		$this->assign('end_date', $end_date);

		$where['user_id']   = $this->user_id;
		$where['is_delete'] = 0;
		// 排序
		$order['create_time'] = 'desc';

		$count = D('FreeOrder')->where($where)->count();
		$list  = D('FreeOrder')->where($where)->limit($first_row . ',' . $pagesize)->order($order)->select();
		foreach ($list as $rs) {
			// 订单时间
			$rs['datetime'] = date('Y-m-d H:i:s', $rs['create_time']);
			$new_list[] = $rs;
		}
		//处理AJAX分页
		$this->page($new_list, $count);
		$this->assign('list', $new_list);
		$this->display();
	}
	// 删除订单
	public function del(){
		$order_sn = $this->_get('order_sn', '对不起，您的操作有误！');

		$where['order_sn']  = $order_sn;
		$where['is_delete'] = 0;
		$info = D('FreeOrder')->where($where)->find();
		if(!$info){
			$this->error('对不起，此订单不存在！');
		}
		if($info['user_id'] != $this->user_id){
			$this->error('对不起，您的操作有误！');
		}
		$data['is_delete']   = 1;
		$data['delete_time'] = time();
		D('FreeOrder')->where(array('order_sn' => $order_sn))->save($data);

		$this->success('删除成功！');
	}
	// 预览合同
	public function preview(){
		$order_sn = $this->_get('order_sn', '对不起，您的操作有误！');
		$info     = D('FreeOrder')->where(array('order_sn' => $order_sn))->find();
		$agreement_id = $info['agreement_id'];
		$doc_content  = D('FreeAgreement')->get_doc_content($this->user_id, $agreement_id, $order_sn);

		$this->assign('info', $info);
		$this->assign('doc_content', $doc_content);
		$this->display();
	}

}
